[ioemu] Cleanup media change code.
authorChristian Limpach <Christian.Limpach@xensource.com>
Mon, 19 Mar 2007 11:15:28 +0000 (11:15 +0000)
committerChristian Limpach <Christian.Limpach@xensource.com>
Mon, 19 Mar 2007 11:15:28 +0000 (11:15 +0000)
Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
tools/ioemu/xenstore.c

index 4f549ac5e3a0ae9e1b5f97c68c18f3cbe6779cfb..26063f5a9395eb982d2a5b58e1fba1c463fc6c62 100644 (file)
@@ -18,7 +18,7 @@
 #include <fcntl.h>
 
 static struct xs_handle *xsh = NULL;
-static char *hd_filename[MAX_DISKS];
+static char *media_filename[MAX_DISKS];
 static QEMUTimer *insert_timer = NULL;
 
 #define UWAIT_MAX (30*1000000) /* thirty seconds */
@@ -45,10 +45,10 @@ static void insert_media(void *opaque)
     int i;
 
     for (i = 0; i < MAX_DISKS; i++) {
-       if (hd_filename[i]) {
-           do_change(bs_table[i]->device_name, hd_filename[i]);
-           free(hd_filename[i]);
-           hd_filename[i] = NULL;
+       if (media_filename[i] && bs_table[i]) {
+           do_change(bs_table[i]->device_name, media_filename[i]);
+           free(media_filename[i]);
+           media_filename[i] = NULL;
        }
     }
 }
@@ -87,7 +87,7 @@ void xenstore_parse_domain_config(int domid)
     unsigned int len, num, hd_index;
 
     for(i = 0; i < MAX_DISKS; i++)
-        hd_filename[i] = NULL;
+        media_filename[i] = NULL;
 
     xsh = xs_daemon_open();
     if (xsh == NULL) {
@@ -133,19 +133,12 @@ void xenstore_parse_domain_config(int domid)
            continue;
        free(type);
        type = xs_read(xsh, XBT_NULL, buf, &len);
-       /* read params to get the patch of the image -- read it last
-        * so that we have its path in buf when setting up the
-        * watch */
        if (pasprintf(&buf, "%s/params", bpath) == -1)
            continue;
        free(params);
        params = xs_read(xsh, XBT_NULL, buf, &len);
        if (params == NULL)
            continue;
-       if (params[0]) {
-           hd_filename[hd_index] = params;     /* strdup() */
-           params = NULL;              /* don't free params on re-use */
-       }
         /* 
          * check if device has a phantom vbd; the phantom is hooked
          * to the frontend device (for ease of cleanup), so lookup 
@@ -156,35 +149,32 @@ void xenstore_parse_domain_config(int domid)
            continue;
        free(fpath);
         fpath = xs_read(xsh, XBT_NULL, buf, &len);
-       if (fpath != NULL) {
+       if (fpath) {
            if (pasprintf(&buf, "%s/dev", fpath) == -1)
                continue;
+           free(params);
             params = xs_read(xsh, XBT_NULL, buf , &len);
-           if (params != NULL) {
-                free(hd_filename[hd_index]);
-                hd_filename[hd_index] = params;
-                params = NULL;              /* don't free params on re-use */
+           if (params) {
                 /* 
                  * wait for device, on timeout silently fail because we will 
                  * fail to open below
                  */
-                waitForDevice(hd_filename[hd_index]);
+                waitForDevice(params);
             }
         }
+
        bs_table[hd_index] = bdrv_new(dev);
-        /* re-establish buf */
-       if (pasprintf(&buf, "%s/params", bpath) == -1)
-           continue;
        /* check if it is a cdrom */
        if (type && !strcmp(type, "cdrom")) {
            bdrv_set_type_hint(bs_table[hd_index], BDRV_TYPE_CDROM);
-           xs_watch(xsh, buf, dev);
+           if (pasprintf(&buf, "%s/params", bpath) != -1)
+               xs_watch(xsh, buf, dev);
        }
-       if (hd_filename[hd_index]) {
-            if (bdrv_open(bs_table[hd_index], hd_filename[hd_index],
-                         0 /* snapshot */) < 0)
+       /* open device now if media present */
+       if (params[0]) {
+            if (bdrv_open(bs_table[hd_index], params, 0 /* snapshot */) < 0)
                 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
-                        hd_filename[hd_index]);
+                        params);
        }
     }
 
@@ -347,13 +337,13 @@ void xenstore_process_event(void *opaque)
 
     do_eject(0, vec[XS_WATCH_TOKEN]);
     bs_table[hd_index]->filename[0] = 0;
-    if (hd_filename[hd_index]) {
-       free(hd_filename[hd_index]);
-       hd_filename[hd_index] = NULL;
+    if (media_filename[hd_index]) {
+       free(media_filename[hd_index]);
+       media_filename[hd_index] = NULL;
     }
 
     if (image[0]) {
-       hd_filename[hd_index] = strdup(image);
+       media_filename[hd_index] = strdup(image);
        xenstore_check_new_media_present(5000);
     }